<?php

namespace App\Http\Controllers\Trade;

use App\Library\Mall\Buyer\BoughtManagers;
use App\Models\Order;
use App\Models\RefundReason;
use Illuminate\Http\Request;

class BoughtController extends BaseController
{
    use BoughtManagers;

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function detail(Request $request)
    {
        $step = 'auction';
        $order_id = $request->get('order_id', 0);
        $order = $this->user()->boughts()->with(['items', 'shipping', 'transaction'])->find($order_id);
        if ($order) {
            if ($order->order_state == 2) {
                $step = 'pay';
            } elseif ($order->order_state == 3) {
                $step = 'send';
            } elseif ($order->order_state == 4 && $order->buyer_rate == 0) {
                $step = 'confirm';
            } elseif ($order->order_state == 4 && $order->buyer_rate == 1) {
                $step = 'rate';
            }
            return $this->view('trade.bought.detail', compact('step', 'order_id', 'order'));
        } else {

            return $this->messager()->setMessage(trans('order.order does not exist'))->render();
        }
    }

    /**
     * @param Request $request
     * @param Order $order
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function confirmOrderSuccess(Request $request, Order $order)
    {
        return $this->messager()->setMessage(trans('order.confirm success'))->render();
    }

    public function showClose(Request $request)
    {
        return $this->view('trade.bought.close');
    }

    /**
     * @param Request $request
     * @param Order $order
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function closedOrderSuccess(Request $request, Order $order)
    {
        return $this->messager()
            ->setMessage(trans('order.order cancel success'))
            ->setRedirectTo(url('trade/bought/detail?order_id='.$order->order_id))->render();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showRefund(Request $request){
        $order_id = $request->input('order_id', 0);
        $order = $this->user()->boughts()->find($order_id);
        $reasons = RefundReason::all();
        return $this->view('trade.bought.refund', compact('reasons', 'order_id', 'order'));
    }

    /**
     * @param Request $request
     * @param Order $order
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function applyRefundOrderSuccess(Request $request, Order $order)
    {
        return $this->messager()
            ->setMessage(trans('order.order refund submit success'))
            ->setRedirectTo(url('trade/bought/detail?order_id='.$order->order_id))->render();
    }
}
